import { defineStore } from 'pinia'

export const useOrderStore = defineStore('order', {
  state: () => ({
    // 订单列表
    orders: [
      {
        id: 'FD13455555333',
        no: 'FD13455555333',
        date: '2025-3-12 12:11:12',
        type: '作业',
        budget: '1000-2000元',
        avatars: [
          '/static/logo.png',
          '/static/logo.png',
          '/static/logo.png',
          '/static/logo.png'
        ],
        signupCount: 30,
        status: 0, // 0:待接单 1:交付中 2:待评价 3:已评价
        description: '需要完成高等数学作业，包含微积分和线性代数部分',
        stage: '本科',
        location: '国内',
        school: '北京大学',
        major: '数学',
        budgetMin: 1000,
        budgetMax: 2000,
        teacherDegree: '硕士',
        abroad: '不要求',
        extra: '希望老师有耐心，能够详细讲解'
      },
      {
        id: 'FD13455555334',
        no: 'FD13455555334',
        date: '2025-3-13 15:30:45',
        type: '论文',
        budget: '2000-3000元',
        avatars: [
          '/static/logo.png',
          '/static/logo.png'
        ],
        signupCount: 12,
        status: 1,
        description: '本科毕业论文指导，计算机科学专业',
        stage: '本科',
        location: '国内',
        school: '清华大学',
        major: '计算机科学',
        budgetMin: 2000,
        budgetMax: 3000,
        teacherDegree: '博士',
        abroad: '不要求',
        extra: '需要论文写作指导和修改建议'
      },
      {
        id: 'FD13455555335',
        no: 'JS13455555333',
        date: '2025-3-12 12:11:12',
        type: '作业',
        budget: '1000-2000元',
        avatars: [
          '/static/logo.png',
          '/static/logo.png',
          '/static/logo.png'
        ],
        signupCount: 25,
        status: 2, // 待评价
        description: '高等数学作业辅导，包含微积分和线性代数相关题目的解答和讲解',
        stage: '本科',
        location: '国内',
        school: '北京大学',
        major: '数学',
        budgetMin: 1000,
        budgetMax: 2000,
        teacherDegree: '硕士',
        abroad: '不要求',
        extra: '希望老师有耐心，能够详细讲解'
      },
      {
        id: 'FD13455555336',
        no: 'JS13455555334',
        date: '2025-3-10 09:15:30',
        type: '作业',
        budget: '1500-2500元',
        avatars: [
          '/static/logo.png',
          '/static/logo.png'
        ],
        signupCount: 18,
        status: 3, // 已评价
        description: '英语作业辅导，包括语法修正、写作指导和口语练习',
        stage: '本科',
        location: '国内',
        school: '复旦大学',
        major: '英语',
        budgetMin: 1500,
        budgetMax: 2500,
        teacherDegree: '硕士',
        abroad: '不要求',
        extra: '希望提高英语写作水平',
        evaluation: {
          overallRating: 4.5,
          professionalRating: 5,
          serviceRating: 4,
          comment: '老师很专业，英语水平很高，讲解很详细，帮助我提高了很多。',
          date: '2025-3-20 16:30:00'
        }
      }
    ],
    
    // 当前订单详情
    currentOrder: null,
    
    // 订单统计
    orderStats: {
      total: 0,
      pending: 0,
      inProgress: 0,
      waitingEvaluation: 0,
      completed: 0
    }
  }),
  
  getters: {
    // 根据状态筛选订单
    getOrdersByStatus: (state) => (status) => {
      return state.orders.filter(order => order.status === status)
    },
    
    // 获取订单统计
    getOrderStats: (state) => {
      const stats = {
        total: state.orders.length,
        pending: 0,
        inProgress: 0,
        waitingEvaluation: 0,
        completed: 0
      }
      
      state.orders.forEach(order => {
        switch (order.status) {
          case 0:
            stats.pending++
            break
          case 1:
            stats.inProgress++
            break
          case 2:
            stats.waitingEvaluation++
            break
          case 3:
            stats.completed++
            break
        }
      })
      
      return stats
    },
    
    // 根据ID获取订单
    getOrderById: (state) => (id) => {
      return state.orders.find(order => order.id === id)
    }
  },
  
  actions: {
    // 添加新订单
    addOrder(orderData) {
      const newOrder = {
        id: 'FD' + Date.now(),
        no: 'FD' + Date.now(),
        date: new Date().toLocaleString(),
        status: 0,
        signupCount: 0,
        avatars: [],
        ...orderData
      }
      
      this.orders.unshift(newOrder)
      return newOrder
    },
    
    // 更新订单状态
    updateOrderStatus(orderId, status) {
      const order = this.orders.find(o => o.id === orderId)
      if (order) {
        order.status = status
      }
    },
    
    // 更新订单报名信息
    updateOrderSignup(orderId, signupCount, avatars) {
      const order = this.orders.find(o => o.id === orderId)
      if (order) {
        order.signupCount = signupCount
        order.avatars = avatars
      }
    },
    
    // 设置当前订单
    setCurrentOrder(order) {
      this.currentOrder = order
    },
    
    // 删除订单
    deleteOrder(orderId) {
      const index = this.orders.findIndex(o => o.id === orderId)
      if (index > -1) {
        this.orders.splice(index, 1)
      }
    },
    
    // 发布新需求
    publishRequirement(formData) {
      const orderData = {
        type: formData.type,
        stage: formData.stage,
        location: formData.location,
        school: formData.school,
        major: formData.major,
        budget: `${formData.budgetMin}-${formData.budgetMax}元`,
        budgetMin: parseInt(formData.budgetMin),
        budgetMax: parseInt(formData.budgetMax),
        teacherDegree: formData.teacherDegree,
        abroad: formData.abroad,
        description: formData.desc,
        extra: formData.extra
      }

      return this.addOrder(orderData)
    },
    
    // 取消订单
    cancelOrder(orderId) {
      this.deleteOrder(orderId)
    },
    
    // 完成订单评价
    completeEvaluation(orderId, evaluation) {
      const order = this.orders.find(o => o.id === orderId)
      if (order) {
        order.status = 3 // 已评价
        order.evaluation = evaluation
      }
    }
  }
})
